package com.dailyblue.java.boot.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.boot.mybatisplus.bean.User;
import com.dailyblue.java.boot.mybatisplus.mapper.UserMapper;
import com.dailyblue.java.boot.mybatisplus.service.UserService;
import com.dailyblue.java.spring.boot.commons.exception.UserCodeNotFoundException;
import com.dailyblue.java.spring.boot.commons.exception.UserPasswordErrorException;
import com.dailyblue.java.spring.boot.commons.util.JsonResult;
import com.dailyblue.java.spring.boot.commons.util.JwtConfig;
import com.dailyblue.java.spring.boot.commons.util.ResultCode;
import com.dailyblue.java.spring.boot.commons.util.ResultTool;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public JsonResult findAllJob() {
        return ResultTool.success(userMapper.findAllJob());
    }

    // USER:ALL::0
    @Cacheable(cacheNames = "USER:ALL", key = "0")
    public JsonResult findAll() {
        return ResultTool.success(super.list());
    }

    @Cacheable(cacheNames = "findUserById", key = "#id")
    public JsonResult findUserById(int id) {
        return ResultTool.success(super.getById(id));
    }

    //@CachePut(cacheNames = "USER:ALL", key = "0")
    @Caching(put = {
            @CachePut(cacheNames = "USER:ALL", key = "0")},
            evict = {
                    @CacheEvict(cacheNames = "findUserById", key = "#id")
            })
    public JsonResult deleteById(int id) {
        boolean b = super.removeById(id);
        return b ? findAll() : ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    @Override
    public List<User> find1() {
        return userMapper.find1();
    }

    @Override
    public JsonResult login(User user) throws UserCodeNotFoundException, UserPasswordErrorException {
        String userCode = user.getUserCode();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        User user1 = userMapper.selectOne(wrapper);
        if (user1 == null) {
            throw new UserCodeNotFoundException("");
        }
        String pass1 = user.getPassword();
        pass1 = DigestUtils.md5Hex(pass1);
        String pass2 = user1.getPassword();
        if (!pass2.equals(pass1)) {
            throw new UserPasswordErrorException("");
        }
        JsonResult result = ResultTool.success(user1, user1.getId() + "");
        String token = JwtConfig.getJwtToken(result);
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        value.set("token:" + user1.getId(), token);
        return result;
    }
}
